<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>领域模型设计模式解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f9f9f9;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
            color: white;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }
        .highlight {
            background: linear-gradient(90deg, rgba(79, 70, 229, 0.1) 0%, rgba(124, 58, 237, 0.1) 100%);
            border-left: 4px solid #4f46e5;
        }
        .feature-icon {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
            width: 60px;
            height: 60px;
            border-radius: 16px;
        }
        .pros-cons-item {
            position: relative;
            padding-left: 1.5rem;
        }
        .pros-cons-item:before {
            content: "";
            position: absolute;
            left: 0;
            top: 0.7rem;
            width: 0.75rem;
            height: 0.75rem;
            border-radius: 50%;
        }
        .pros:before {
            background-color: #10b981;
        }
        .cons:before {
            background-color: #ef4444;
        }
        @media (max-width: 768px) {
            .hero-content {
                padding-top: 3rem;
                padding-bottom: 3rem;
            }
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero min-h-screen flex items-center">
        <div class="container mx-auto px-4 hero-content">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">领域模型设计模式</h1>
                <p class="text-xl md:text-2xl opacity-90 mb-8">探索贫血模型与充血模型的精髓</p>
                <div class="text-lg mb-12 max-w-2xl mx-auto">
                    领域模型是面向对象设计的核心，贫血模型与充血模型作为两种经典实现方式，各有其适用场景与优劣。本文将深入解析这两种模式的本质区别与实践应用。
                </div>
                <div class="flex justify-center space-x-4">
                    <a href="#anemic" class="px-6 py-3 bg-white text-indigo-600 rounded-lg font-medium hover:bg-opacity-90 transition">贫血模型</a>
                    <a href="#rich" class="px-6 py-3 bg-indigo-800 text-white rounded-lg font-medium hover:bg-opacity-90 transition">充血模型</a>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-4 py-16 max-w-6xl">
        <!-- Introduction -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="feature-icon flex items-center justify-center mr-6">
                    <i class="fas fa-project-diagram text-white text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold">领域模型概述</h2>
            </div>
            
            <div class="bg-white rounded-xl p-6 shadow-md mb-8">
                <p class="text-lg mb-6">领域模型是面向对象设计中的一个重要概念，它指的是对业务领域的抽象模型，通常由类、对象及它们之间的关系组成。领域模型旨在捕捉和表达业务逻辑和业务规则，通过这些模型使得开发者能够在代码中实现业务需求并解决业务问题。</p>
                
                <div class="grid md:grid-cols-3 gap-6 mt-8">
                    <div class="bg-gray-50 p-6 rounded-lg">
                        <h3 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-bullseye mr-2 text-indigo-500"></i>业务导向
                        </h3>
                        <p>领域模型主要关注业务需求和业务规则，而不是技术实现。</p>
                    </div>
                    <div class="bg-gray-50 p-6 rounded-lg">
                        <h3 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-boxes mr-2 text-indigo-500"></i>抽象封装
                        </h3>
                        <p>通过抽象和封装，将业务逻辑和数据紧密结合，确保业务逻辑的完整性和一致性。</p>
                    </div>
                    <div class="bg-gray-50 p-6 rounded-lg">
                        <h3 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-graduation-cap mr-2 text-indigo-500"></i>领域知识
                        </h3>
                        <p>领域模型包含领域专家的知识，并在设计中体现这些知识。</p>
                    </div>
                </div>
            </div>
            
            <!-- Visualization -->
            <div class="mt-12 bg-white rounded-xl p-6 shadow-md">
                <h3 class="text-xl font-semibold mb-6">领域模型核心概念关系</h3>
                <div class="mermaid">
                    graph TD
                    A[业务需求] --> B[领域模型]
                    B --> C[贫血模型]
                    B --> D[充血模型]
                    C --> E[业务逻辑在服务层]
                    D --> F[业务逻辑在领域对象]
                    E --> G[灵活性高]
                    E --> H[可能分散]
                    F --> I[封装性好]
                    F --> J[一致性高]
                </div>
            </div>
        </section>

        <!-- Anemic Model -->
        <section id="anemic" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="feature-icon flex items-center justify-center mr-6">
                    <i class="fas fa-layer-group text-white text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold">贫血模型</h2>
            </div>
            
            <div class="bg-white rounded-xl p-6 shadow-md mb-8">
                <p class="text-lg mb-6">贫血模型是指领域模型中，领域对象主要用作数据容器，业务逻辑则放置在服务类或其他业务逻辑类中。领域对象通常只包含数据字段和简单的 getter/setter 方法，而没有复杂的业务逻辑。业务逻辑由服务类负责，实现这些逻辑的处理。</p>
                
                <div class="highlight p-6 rounded-lg my-8">
                    <h3 class="text-xl font-semibold mb-4">典型案例结构</h3>
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <h4 class="font-medium mb-2">领域对象</h4>
                            <pre class="bg-gray-800 text-gray-100 p-4 rounded-lg text-sm overflow-x-auto">
public class Order {
    private Long id;
    private Date createTime;
    private Double amount;
    
    // getters and setters...
}</pre>
                        </div>
                        <div>
                            <h4 class="font-medium mb-2">服务类</h4>
                            <pre class="bg-gray-800 text-gray-100 p-4 rounded-lg text-sm overflow-x-auto">
public class OrderService {
    public void applyDiscount(Order order, Double discount) {
        if (order.getAmount() > 100) {
            order.setAmount(order.getAmount() * discount);
        }
    }
}</pre>
                        </div>
                    </div>
                </div>
                
                <h3 class="text-xl font-semibold mb-4">特点</h3>
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="border border-gray-200 rounded-lg p-4">
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-check-circle mr-2 text-green-500"></i>领域对象简单
                        </h4>
                        <p>领域对象仅包含数据字段和简单的访问方法，业务逻辑分散在服务层。</p>
                    </div>
                    <div class="border border-gray-200 rounded-lg p-4">
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-check-circle mr-2 text-green-500"></i>业务逻辑集中
                        </h4>
                        <p>业务逻辑通常放在专门的服务类中，与领域对象分离。</p>
                    </div>
                    <div class="border border-gray-200 rounded-lg p-4">
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-check-circle mr-2 text-green-500"></i>灵活性
                        </h4>
                        <p>可能提高了灵活性和可重用性，但可能导致领域逻辑的分散和代码复杂性增加。</p>
                    </div>
                </div>
                
                <h3 class="text-xl font-semibold mb-4">优缺点</h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div>
                        <h4 class="text-lg font-medium mb-3 text-green-600">优点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item pros">易于实现和维护，尤其是在业务逻辑复杂且变动频繁的情况下</li>
                            <li class="pros-cons-item pros">业务逻辑和领域模型分离，有助于解耦</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="text-lg font-medium mb-3 text-red-600">缺点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item cons">可能导致领域对象缺乏业务逻辑，逻辑集中在服务层中，可能使代码更加难以管理和理解</li>
                            <li class="pros-cons-item cons">业务规则和数据管理分离可能导致数据一致性问题</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="mt-6 bg-white rounded-xl p-6 shadow-md">
                <h3 class="text-xl font-semibold mb-4">适用场景</h3>
                <div class="grid md:grid-cols-3 gap-4">
                    <div class="bg-blue-50 p-4 rounded-lg">
                        <div class="flex items-center mb-2">
                            <i class="fas fa-random text-blue-500 mr-2"></i>
                            <h4 class="font-medium">简单业务</h4>
                        </div>
                        <p>业务逻辑相对简单，不需要复杂的领域对象</p>
                    </div>
                    <div class="bg-blue-50 p-4 rounded-lg">
                        <div class="flex items-center mb-2">
                            <i class="fas fa-users text-blue-500 mr-2"></i>
                            <h4 class="font-medium">团队能力</h4>
                        </div>
                        <p>团队对面向对象设计理解有限</p>
                    </div>
                    <div class="bg-blue-50 p-4 rounded-lg">
                        <div class="flex items-center mb-2">
                            <i class="fas fa-sync-alt text-blue-500 mr-2"></i>
                            <h4 class="font-medium">频繁变更</h4>
                        </div>
                        <p>业务规则需要频繁变更</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Rich Model -->
        <section id="rich" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="feature-icon flex items-center justify-center mr-6">
                    <i class="fas fa-box-open text-white text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold">充血模型</h2>
            </div>
            
            <div class="bg-white rounded-xl p-6 shadow-md mb-8">
                <p class="text-lg mb-6">充血模型是指领域对象不仅包含数据字段，还包含处理这些数据的业务逻辑。领域对象不仅负责数据的存储和访问，还承担着业务逻辑的处理任务。充血模型将业务逻辑紧密集成到领域对象中，使得领域对象成为业务逻辑的主要载体。</p>
                
                <div class="highlight p-6 rounded-lg my-8">
                    <h3 class="text-xl font-semibold mb-4">典型案例结构</h3>
                    <div class="grid grid-cols-1">
                        <div>
                            <h4 class="font-medium mb-2">领域对象</h4>
                            <pre class="bg-gray-800 text-gray-100 p-4 rounded-lg text-sm overflow-x-auto">
public class Order {
    private Long id;
    private Date createTime;
    private Double amount;
    
    // getters and setters...
    
    public void applyDiscount(Double discount) {
        if (this.amount > 100) {
            this.amount *= discount;
        }
    }
}</pre>
                        </div>
                    </div>
                </div>
                
                <h3 class="text-xl font-semibold mb-4">特点</h3>
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="border border-gray-200 rounded-lg p-4">
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-check-circle mr-2 text-green-500"></i>领域对象复杂
                        </h4>
                        <p>领域对象包含数据字段以及与数据相关的业务逻辑。</p>
                    </div>
                    <div class="border border-gray-200 rounded-lg p-4">
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-check-circle mr-2 text-green-500"></i>业务逻辑封装
                        </h4>
                        <p>业务逻辑被封装在领域对象中，使得领域模型更具业务语义。</p>
                    </div>
                    <div class="border border-gray-200 rounded-lg p-4">
                        <h4 class="font-medium mb-2 flex items-center">
                            <i class="fas fa-check-circle mr-2 text-green-500"></i>业务规则一致性
                        </h4>
                        <p>通过将业务规则和数据封装在一起，有助于确保业务规则的一致性和完整性。</p>
                    </div>
                </div>
                
                <h3 class="text-xl font-semibold mb-4">优缺点</h3>
                <div class="grid md:grid-cols-2 gap-6">
                    <div>
                        <h4 class="text-lg font-medium mb-3 text-green-600">优点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item pros">提高了领域模型的表达力和业务逻辑的封装性，有助于保持业务规则的一致性</li>
                            <li class="pros-cons-item pros">可以减少数据和业务逻辑的分离带来的复杂性</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="text-lg font-medium mb-3 text-red-600">缺点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item cons">可能导致领域对象变得复杂，增加了开发和维护的难度</li>
                            <li class="pros-cons-item cons">业务逻辑和数据操作紧密耦合，可能对领域对象的变化带来较大的影响</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <div class="mt-6 bg-white rounded-xl p-6 shadow-md">
                <h3 class="text-xl font-semibold mb-4">适用场景</h3>
                <div class="grid md:grid-cols-3 gap-4">
                    <div class="bg-purple-50 p-4 rounded-lg">
                        <div class="flex items-center mb-2">
                            <i class="fas fa-brain text-purple-500 mr-2"></i>
                            <h4 class="font-medium">复杂业务</h4>
                        </div>
                        <p>业务逻辑复杂且相对稳定</p>
                    </div>
                    <div class="bg-purple-50 p-4 rounded-lg">
                        <div class="flex items-center mb-2">
                            <i class="fas fa-user-tie text-purple-500 mr-2"></i>
                            <h4 class="font-medium">团队能力</h4>
                        </div>
                        <p>团队具备良好的面向对象设计能力</p>
                    </div>
                    <div class="bg-purple-50 p-4 rounded-lg">
                        <div class="flex items-center mb-2">
                            <i class="fas fa-shield-alt text-purple-500 mr-2"></i>
                            <h4 class="font-medium">数据一致性</h4>
                        </div>
                        <p>对数据一致性和业务规则完整性要求高</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison -->
        <section class="mb-12">
            <div class="flex items-center mb-8">
                <div class="feature-icon flex items-center justify-center mr-6">
                    <i class="fas fa-balance-scale text-white text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold">模型对比</h2>
            </div>
            
            <div class="bg-white rounded-xl p-6 shadow-md">
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"></th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">贫血模型</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">充血模型</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium">业务逻辑位置</td>
                                <td class="px-6 py-4 whitespace-nowrap">服务层</td>
                                <td class="px-6 py-4 whitespace-nowrap">领域对象内部</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium">领域对象职责</td>
                                <td class="px-6 py-4 whitespace-nowrap">数据容器</td>
                                <td class="px-6 py-4 whitespace-nowrap">数据和业务逻辑</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium">代码复杂度</td>
                                <td class="px-6 py-4 whitespace-nowrap">服务层复杂</td>
                                <td class="px-6 py-4 whitespace-nowrap">领域对象复杂</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium">数据一致性</td>
                                <td class="px-6 py-4 whitespace-nowrap">可能不一致</td>
                                <td class="px-6 py-4 whitespace-nowrap">高度一致</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium">维护性</td>
                                <td class="px-6 py-4 whitespace-nowrap">业务变更容易</td>
                                <td class="px-6 py-4 whitespace-nowrap">领域变更影响大</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium">团队要求</td>
                                <td class="px-6 py-4 whitespace-nowrap">入门友好</td>
                                <td class="px-6 py-4 whitespace-nowrap">需要OOP经验</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </section>

        <!-- Decision Guide -->
        <section>
            <div class="flex items-center mb-8">
                <div class="feature-icon flex items-center justify-center mr-6">
                    <i class="fas fa-question-circle text-white text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold">如何选择?</h2>
            </div>
            
            <div class="bg-white rounded-xl p-6 shadow-md">
                <div class="mermaid">
                    flowchart TD
                    A[开始] --> B{业务复杂度}
                    B -->|简单| C[贫血模型]
                    B -->|复杂| D{变更频率}
                    D -->|高| C
                    D -->|低| E{团队能力}
                    E -->|强| F[充血模型]
                    E -->|弱| C
                    C --> G[决策完成]
                    F --> G
                </div>
                
                <div class="mt-8 grid md:grid-cols-2 gap-6">
                    <div class="bg-indigo-50 p-6 rounded-lg">
                        <h3 class="text-xl font-semibold mb-4 text-indigo-700">选择贫血模型当...</h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>业务逻辑简单直接</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>业务规则经常变化</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>团队成员面向对象经验有限</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-indigo-500 mt-1 mr-2"></i>
                                <span>需要快速开发和原型设计</span>
                            </li>
                        </ul>
                    </div>
                    <div class="bg-purple-50 p-6 rounded-lg">
                        <h3 class="text-xl font-semibold mb-4 text-purple-700">选择充血模型当...</h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>业务逻辑复杂且具有丰富行为</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>业务规则相对稳定</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>团队具备良好的面向对象设计能力</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>对数据一致性和完整性要求高</span>
                            </li>
                        </ul>
                    </div>
                </div>
                
                <div class="mt-8 p-6 bg-yellow-50 rounded-lg">
                    <h3 class="text-xl font-semibold mb-4 text-yellow-700 flex items-center">
                        <i class="fas fa-lightbulb mr-2"></i>
                        <span>实践建议</span>
                    </h3>
                    <div class="grid md:grid-cols-2 gap-6">
                        <div>
                            <h4 class="font-medium mb-2">混合使用</h4>
                            <p>在实际项目中，可以考虑混合使用两种模式。对核心复杂领域使用充血模型，对简单辅助领域使用贫血模型。</p>
                        </div>
                        <div>
                            <h4 class="font-medium mb-2">渐进式演进</h4>
                            <p>可以从贫血模型开始，随着对业务理解的深入和团队能力的提升，逐步向充血模型演进。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>